package com.crazyhat.crazyhat.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.crazyhat.crazyhat.entity.Activity;
import com.crazyhat.crazyhat.pojo.ActivityPublish;
import com.crazyhat.crazyhat.pojo.ActivitySimple;
import com.crazyhat.crazyhat.pojo.Result;
import com.crazyhat.crazyhat.service.IActivityService;
import com.crazyhat.crazyhat.vaildation.ValidationGroup.InsertValidAtionGroup;
import com.crazyhat.crazyhat.vaildation.ValidationGroup.UpdateValidAtionGroup;




/**
 * <p>
 * 社团活动 前端控制器
 * </p>
 *
 * @author crazyhat
 * @since 2024-07-05
 */
@RestController
@RequestMapping("/activity")
public class ActivityController {

    @Autowired
    private IActivityService activityService;

    /**
     * 获取全部社团活动，分页
     * @param culbId
     * @return
     */
    @GetMapping("/ActivityList")
    public Result<Page<ActivitySimple>> getActivityList(
            @RequestParam(value = "clubId") Integer clubId,
            @RequestParam(value = "pageNum") Integer pageNum,
            @RequestParam(value = "pageSize") Integer pageSize
            ) {

        Page<ActivitySimple> activitySimpleList = activityService.getActivityList(clubId, pageNum, pageSize);

        if (activitySimpleList!=null) {
            return Result.success(activitySimpleList);
        } else {
            return Result.error("获取活动列表失败");
        }
    }

    /**
     * 获取进行中的活动的列表，分页
     * @param clubId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/ActivityListInProgress")
    public Result<Page<ActivitySimple>> getActivityListInProgress(
        @RequestParam(value = "clubId") Integer clubId,
        @RequestParam(value = "pageNum") Integer pageNum,
        @RequestParam(value = "pageSize") Integer pageSize
        ) {

        Page<ActivitySimple> activitySimpleList = activityService.getActivityListInProgress(clubId, pageNum, pageSize);


        if (activitySimpleList!=null) {
            return Result.success(activitySimpleList);
        } else {
            return Result.error("获取进行中的活动列表失败");
        }
    }

    /**
     * 获取活动信息
     * @param activityId
     * @return
     */
    @GetMapping("/activityInfo")
    public Result<Activity> getActivity(@RequestParam(value = "activityId") String activityId) {

        return Result.success(activityService.getById(activityId));
    }

    /**
     * 发布活动
     * @param clubId
     * @param activityName
     * @param content
     * @param fileList
     * @return
     */
    @PostMapping(value = "/publishActivity", consumes = "multipart/form-data")
    public Result<Activity> publishActivityWithFile(
            @ModelAttribute @Validated(InsertValidAtionGroup.class) ActivityPublish activity
        ) {
        Activity newActivity = activityService.publishActivity(activity);

        if (newActivity!=null) {
            return Result.success(newActivity);
        } else {
            return Result.error("发布活动失败");
        }
    }


    /**
     * 删除社团活动，仅社团管理员拥有权限
     * @param activityId
     * @return
     */
    @PostMapping("/deleteActivity")
    public Result deleteActivity(@RequestParam(value = "activityId") Integer activityId){

        boolean isSuccess = activityService.deleteActivity(activityId);

        if (isSuccess) {
            return Result.success();
        } else {
            return Result.error("删除活动失败");
        }
    }

    /**
     * 修改活动
     * @param activityId
     * @param activity
     * @return
     */
    @PostMapping("/updateActivity")
    public Result<Activity> updateActivity(@ModelAttribute @Validated(UpdateValidAtionGroup.class) ActivityPublish activity) {

        Activity updatedActivity=activityService.updateActivity(activity);

        if (updatedActivity!=null) {
            return Result.success(updatedActivity);
        } else {
            return Result.error("更新活动失败");
        }
    }

    /**
     * 用户是否对已发布的活动有编辑权限
     * @param activityId
     * @return
     */
    @GetMapping("/activityEditPower")
    public Result<Boolean> hasactivityEditPower(@RequestParam(value = "activityId") Integer activityId) {

        boolean is=activityService.hasActivityEditPower(activityId);

        return Result.success(is);
    }
    
}
